package com.yyk.yiliao.ui.find.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseFragment;
import com.yyk.yiliao.adapter.RecyclerViewHome_Adapter;
import com.yyk.yiliao.ui.gongyong.activity.WebViewActivity;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.bean.HomeGetads_Info;
import com.yyk.yiliao.widget.EndLessOnScrollListener;
import com.yyk.yiliao.widget.diver.CustomDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ${zk} on 2018/8/6 0006.
 * <p>
 */

public class Find_TabFragment extends BaseFragment {
    public static final String ARGS_PAGE = "Find_TabFragment";
    @BindView(R.id.nTab_rv)
    RecyclerView nTab_rv;
    private String type;
    private RecyclerViewHome_Adapter adapter;
    private int pageIndex;
    private List<HomeGetads_Info.DataBean> data = new ArrayList<>();

    public static Find_TabFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARGS_PAGE, page);
        Find_TabFragment fragment = new Find_TabFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_tab;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        initGetIntent();
        initAdapter();
        initPost();
    }

    private void initAdapter() {
        adapter = new RecyclerViewHome_Adapter(getActivity(), data);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        nTab_rv.setLayoutManager(mLinearLayoutManager);
        nTab_rv.addItemDecoration(new CustomDecoration(getActivity(), CustomDecoration.VERTICAL_LIST, R.drawable.divider_white_ef_1px, 0));
        nTab_rv.setAdapter(adapter);
        nTab_rv.addOnScrollListener(new EndLessOnScrollListener(mLinearLayoutManager) {
            @Override
            public void onLoadMore(int currentPage) {
//                if (pageIndex == currentPage) {
//                    return;
//                }
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        //请求网络
                        pageIndex++;
                        setHomeGetads(false, pageIndex);

                    }
                }, 1000);
            }
        });
        adapter.setOnItemClickListener(new RecyclerViewHome_Adapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                Intent intent = new Intent(getActivity(), WebViewActivity.class);
                intent.putExtra("toolbar_tv", "发现详情");
                intent.putExtra("url", ApiService.H5_URL + "views/newDiscover/DiscoverInfo.html?tid=" + data.get(position).getId() + "&Sources=3?from=app");
                startActivity(intent);
            }
        });
    }


    private void initGetIntent() {
        Bundle arguments = getArguments();
        type = arguments.getString("Find_TabFragment");
        Logger.e("type——————" + type);
    }

    private void initPost() {
        pageIndex = 1;
        setHomeGetads(true, pageIndex);
    }

    private void setHomeGetads(final boolean flag, int pageIndex) {
        Map<String, String> map = new TreeMap<>();
        map.put("page", pageIndex + "");
        map.put("type", type);
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postHomeGetads(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<HomeGetads_Info>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(HomeGetads_Info info) {
                        if (info.getCode() == 1) {
                            if (flag) {
                                data.clear();
                            }
                            if (info.getData().size() > 0 && info.getData() != null) {
                                data.addAll(info.getData());
                            }
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
    }


}
